package com.gomeplus.jdbc.page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageQueryEntitys extends Entitys {

	private static final long serialVersionUID = 1520062757473541274L;

	private Map<String, String> f = new HashMap<String, String>();
	private int page = 1; // 当前页数
	private int rows = 10; // 每页展示记录数
	private int totalCount = -1; // 总记录数
	private int totalPages; // 总页数
	private int limit; // 开始检索数
	private int offset; // 检出记录数
	private List<Entitys> list = null; // 返回记录集合
	private String[] strArray = null;

	/**
	 * 添加过滤器,paramKey为request传递的parameter的key, 对应页面表单中输入项的name,用于对原有查询进行限定
     *
     * @param paramKey 字段名称
     * @param value 字段值
     */
    public void addF(String paramKey, String value) {
        if (value != null && value.length() > 0) {
			this.f.put(paramKey, value);
		}
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 当前页.
	 */
	public int getPage() {
		if (page < 1) {
            page = 1;
        }
		return page;
	}

	/**
	 * 设置当前页.
	 */
	public void setPage(final int page) {
		this.page = page;

	}

	/**
	 * 首记录.
	 */
	public int getFirst() {
		return ((getPage() - 1) * rows) + 1;
	}

	/**
	 * 尾记录.
	 */
	public int getLast() {
		return (getFirst() + rows) > getTotalCount() ? getTotalCount() : (getFirst() + rows - 1);
	}

	public int getTotalPages() {
		int count = (totalCount + rows - 1) / rows;
		return count;
	}

	public void setTotalPages(int totalPages) {
		if (totalPages == 0) {
			totalPages = 1;
		}
		this.totalPages = totalPages;
	}

	/**
	 * 重置总记录数
	 */
	public void resetTotalCount(int totalCount) {
		if (totalCount < 0) {
			totalCount = 0;
		}
		int count = (totalCount + rows - 1) / rows;
		setTotalCount(totalCount);
		setTotalPages(count);
		if (getPage() > getTotalPages()) {
			setPage(getTotalPages());
		}
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext() {
		return (page + 1 <= getTotalPages());
	}

	/**
	 * 下一页页号.
	 */
	public int getNextPage() {
		if (isHasNext()) {
			return page + 1;
		} else {
			return page;
		}
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPre() {
		return (page - 1 >= 1);
	}

	/**
	 * 上一页页号.
	 */
	public int getPrePage() {
		if (isHasPre()) {
			return page - 1;
		} else {
			return page;
		}
	}

	/**
	 * @Title: getBeginCount
	 * @Description: 计算最后一面的记录数
	 * @return int
	 */
	public int getBeginCount() {
		int beginCount = (getPage() - 1) * rows;
		if (beginCount >= this.getTotalCount()) {
			int modpageSize = this.getTotalCount() % this.rows;
			if (modpageSize == 0) {
				modpageSize = this.rows;
			}
			beginCount = this.getTotalCount() + 1 - modpageSize;
		}
		return beginCount < 0 ? 0 : beginCount;
	}

	public int getPageRows(){
		return rows;
	}

	public Map<String, String> getF() {
		return f;
	}

	public void setF(Map<String, String> f) {
		this.f = f;
	}

	/**
	 * @Title: getLimit
	 * @Description: 计算从第几条开始取
	 * @return int
	 */
	public int getLimit() {
		limit = page * rows - rows;
		return limit;
	}

	/**
	 * @Title: getOffset
	 * @Description: 计算总共取多少条
	 * @return int
	 */
	public int getOffset() {
		if (page >= totalPages && page != 1) {
			offset = getBeginCount();
		} else {
			offset = rows;
		}
		return offset;
	}

	public List<Entitys> getList() {
		return list;
	}

	public void setList(List<Entitys> list) {
		this.list = list;
	}

	public String[] getStrArray() {
		return strArray;
	}

	public void setStrArray(String[] strArray) {
		this.strArray = strArray;
	}

    /**
     * 设置查询参数
     *
     * @param rows       每页数目
     * @param page       页码
     * @param totalCount 总数
     */
    public void setPageParameters(int rows, int page, int totalCount) {
        this.setRows(rows);
        this.setPage(page);
        this.resetTotalCount(totalCount);
    }
}
